/**
 * 客户列表
*/

var wawa_type = new Object({
    table:null,
    init:function(){
        if(null != this.table)
            return;
        this.queryInfo();
        this.bindEvent();
    },
    queryInfo:function(){
        this.table = $('#tb_wawa_type').bootstrapTable({
            url: contextPath + '/wawa/type/listData.json',
            responseHandler: function (res) {
                return res.data;
            },
            queryParams: function (params) {
                return params;
            },
            pagination: true,
            pageList: '[10,20, 50, 100]',
            //showColumns: true,
            showRefresh: true,
            showToggle: true,
            //showPaginationSwitch:true,
            sidePagination: 'server',
            //striped:true,
            sortable: false,
            height: 700,
            search: true,
            searchAlign: 'left',
            toolbar: '<div style="float: left;margin-right:5px;">' +
            '    <button class="btn btn-default" type="button" onclick="wawa_type.add()">' +
            '        <i style="margin-right:2px;" class="fa fa-search"></i>' +
            '        <span>新增</span>' +
            '    </button>' +
            '</div>',
            idField: 'id',
            uniqueId: 'id',
            //singleSelect: true,
            clickToSelect: true,
            columns: [{
                field: 'id',
                title: 'ID',
                width: 100,
                visible:false
            }, {
                field: 'typeName',
                title: '类型名称',
                width: 100
            }, {
                field: 'typeCode',
                title: '类型编号',
                width: 100
            }, {
                field: 'thumbnailUrl',
                title: '缩略图',
                width: 100,
                formatter:function(value,row,index){
                    return '<img src="' + value + '" height="30" width="30" />';
                }
            }, {
                field: 'payItemCount',
                title: '消耗金币数',
                width: 100
            }, {
                field: 'bigPicUrl',
                title: '大图地址',
                width: 100,
                formatter:function(value,row,index){
                    return '<a href="'+value+'" target="_blank">查看</a>'
                }
            },{
                field: 'op',
                title: '操作',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    var strHtml = '';
                        //strHtml += '<a href="javascript:void(0);" onclick="client_list.edit('+row.id+',\''+row.name+'\','+row.ratio+')">编辑</a>&nbsp;&nbsp;';
                    strHtml = '<div class="btn-group">' +
                    '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">更多<i class="fa fa-caret-down"></i></button>' +
                    '   <ul class="dropdown-menu">'+
                    '       <li><a href="javascript:void(0);" onclick="wawa_type.editDetails('+row.id+')">编辑</a></li>'+
                    '       <li><a href="javascript:void(0);" onclick="wawa_type.deleteType('+row.id+')">删除</a></li>'+
                    '   </ul>'+
                    '</div>'
                    return strHtml;
                }
            }]
        });
        $('.search input').attr("placeholder", "名称");
    },
    bindEvent: function () {
        $('#upload_w_img').on('clearInput',function(e){
            $("#wawa_type_img").attr("src",wawa_type.kongbai_img)
        })

        $('#upload_w_img').die().live('change',function(){
            $.ajaxFileUpload({
                url:contextPath+'/file/upload',
                secureuri:false,
                fileElementId:'upload_w_img',//file标签的id
                dataType: 'json',//返回数据的类型
                //data:{name:'logan'},//一同上传的数据
                success: function (res, status) {
                    //把图片替换
                    $("#wawa_type_img").attr("src",res.data.file_thumbnail_url)
                    $('#w_pic_url').attr('value',res.data.file_url);
                    $('#w_thumbnail_url').attr('value',res.data.file_thumbnail_url);
                    if(typeof(res.error) != 'undefined') {
                        if(res.error != '') {
                            alert(res.error);
                        } else {
                            alert(res.msg);
                        }
                    }
                },
                error: function (data, status, e) {
                    alert(e);
                }
            });
        })
    },
    kongbai_img:'http://static-wwj.ballq.cn/upload/images/kongbai.png',
    add:function(){
        var addWaWaTypeModal = '<div id="addWaWaTypeModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="setWxInfoModalLabel">编辑娃娃类型</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="addWaWaTypeForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">类型名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="typeName" value="" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">类型编号：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="typeCode" value="" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">消耗金币数：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="payItemCount" value="" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <input type="hidden" name="bigPicUrl" id="w_pic_url" value=""/>'+
            '                   <input type="hidden" name="thumbnailUrl" id="w_thumbnail_url" value=""/>'+
            '               </form>'+
            '               <div class="form-group">'+
            '                   <label class="col-sm-3 control-label">图片：</label>'+
            '                   <div class="col-sm-9" style="padding-top:7px;">' +
            '                       <input type="file" id="upload_w_img" name="upload" class="file" multiple data-min-file-count="1"/>' +
            '                       <div class="dropzone-box" style="text-align:center">' +
            '                           <img src="'+wawa_type.kongbai_img+'" id="wawa_type_img" style="width:100%;height:100%"/>'+
            '                      </div>' +
            '               </div>'+
            '           </div>' +
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="wawa_type.addSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //console.info(addWaWaTypeModal);
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(addWaWaTypeModal)[0]);
        $('#addWaWaTypeModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#addWaWaTypeModal')[0]);
        });

        //$('#upload_img').pixelFileInput({ placeholder: '没有选择文件...' });

        wawa_type.bindEvent();

        //显示
        $("#addWaWaTypeModal").modal('show')
    },
    addSubmit:function(){
        $.ajax({
            url:contextPath+'/wawa/type/add',
            method:'post',
            data:$('#addWaWaTypeForm').serialize(),
            success:function(res){
                wawa_type.table.bootstrapTable('refresh');
                $('#addWaWaTypeModal').modal('hide');
            },
            error: function () {
                alert_message("添加出错，请联系管理员")
            }
        });

    },
    editDetails:function(id){
        var res = "";
        $.ajax({
            url:contextPath+'/wawa/type/findById/'+id,
            method:'get',
            async:false,
            success:function(data){
                //获取数据
                res = data;
            },
            error:function(){
                alert_message('请求错误，请联系管理员');
            }
        });
        var editWaWaTypeModal = '<div id="editWaWaTypeModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="setWxInfoModalLabel">编辑娃娃类型</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="editWaWaTypeForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">类型名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="typeName" value="'+res.data.typeName+'" />'+
            '                           <input type="hidden" class="form-control" name="id" value="'+res.data.id+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">类型编号：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="typeCode" value="'+res.data.typeCode+'" />'+
            '                           <input type="hidden" class="form-control" name="id" value="'+res.data.id+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">消耗金币数：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="payItemCount" value="'+res.data.payItemCount+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <input type="hidden" name="bigPicUrl" id="w_pic_url" value="'+res.data.bigPicUrl+'"/>'+
            '                   <input type="hidden" name="thumbnailUrl" id="w_thumbnail_url" value="'+res.data.thumbnailUrl+'"/>'+
            '               </form>'+
                //'               <form method="post" enctype="multipart/form-data" class="form-horizontal">' +
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">图片：</label>'+
            '                       <div class="col-sm-9" style="padding-top:7px;">' +
            '                           <input type="file" id="upload_w_img" name="upload" class="file" multiple data-min-file-count="1"/>' +
            '                           <div class="dropzone-box" style="text-align:center">' +
            '                               <img src="'+res.data.thumbnailUrl+'" id="wawa_type_img" style="width:100%;height:100%"/>'+
            '                          </div>' +
            '                   </div>'+
                //'               </form>'+
            '           </div>' +
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="wawa_type.editDetailsSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //alert(editWaWaTypeModal);

        //显示
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(editWaWaTypeModal)[0]);
        $('#editWaWaTypeModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#editWaWaTypeModal')[0]);
        });

        //绑定事件
        wawa_type.bindEvent();

        //显示
        $('#editWaWaTypeModal').modal('show');


    },
    editDetailsSubmit:function(){
        $.ajax({
            url:contextPath+'/wawa/type/update',
            data:$('#editWaWaTypeForm').serialize(),
            method:'post',
            success:function(res){
                alert_message("修改成功");
                wawa_type.table.bootstrapTable('refresh');
                $('#editWaWaTypeModal').modal('hide');
            },
            error:function(){
                alert_message("修改出错，请联系管理员");
            }
        });
    },
    deleteType:function(id){
        bootbox.confirm('确认要删除娃娃类型吗？',function(result){
                if(result) {
                    $.ajax({
                        url: contextPath + '/wawa/type/deleteById/' ,
                        method: 'post',
                        data:{id:id},
                        success: function (res) {
                            wawa_type.table.bootstrapTable('refresh');
                            if(res.meta.success){
                                alert_message(res.data);
                            }else{
                                alert_message(res.meta.message);
                            }
                        },
                        error: function () {
                            alert_message("删除出错，请联系管理员");
                        }
                    });
                }
            }
        );
    }

});